package concurrent

import (
	"fmt"
	"runtime"
	"sync"
)

const (
	name = iota
	name2
	name3
	name4
	name5
)

func Print() {
	test6()
}

func test1() {
	c := make(chan bool)
	go func() {
		c <- true
	}()
	fmt.Println(<-c)
}

func test2() {
	c := make(chan bool, 10)
	preCpu := runtime.GOMAXPROCS(runtime.NumCPU())
	fmt.Println("preCpu", preCpu)
	for i := 0; i < 10; i++ {
		go func() {
			fmt.Println(i)
			c <- true
		}()
	}

	for i := 0; i < 10; i++ {
		<-c
	}

}

func test3() {
	c := make(chan bool, 10)
	wg := sync.WaitGroup{}
	wg.Add(10)
	for i := 0; i < 10; i++ {
		j := i
		go func() {
			fmt.Println(j)
			c <- true
			wg.Done()
		}()
	}
	wg.Wait()
	fmt.Println("end")
}

func test4() {
	c2 := make(chan string, 10)
	for true {
		select {
		case i := <-c2:
			fmt.Println("receive", i)
		case c2 <- "wwy":
			fmt.Println("send")
		default:
			fmt.Println("default")
		}
	}
}

func test5() {
	cs := make([]chan string, 3)
	for i := 0; i < 3; i++ {
		cs[i] = make(chan string, 10)
		cs[i] <- "wwy"
	}
	select {
	case i := <-cs[0]:
		fmt.Println(0, i)
	case i := <-cs[1]:
		fmt.Println(1, i)
	case i := <-cs[2]:
		fmt.Println(2, i)
	}

}

func test6() {
	c := make(chan string)
	go func() {
		for true {
			c <- "hello"
			i := <-c
			fmt.Println(i)
		}

	}()
	for true {
		i := <-c
		fmt.Println(i)
		c <- "hi"
	}
}
